<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="1200px" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="150px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="物料编码" prop="materialNo">
              <el-input v-model="formData.materialNo" placeholder="请输入物料编码" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="formData.materialName" placeholder="请输入物料名称" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料属性" prop="materialType">
              <el-select v-model="formData.materialType" placeholder="请选择物料属性" filterable style="width: 100%"
                         @change="handleMaterialTypeChange">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MATERIAL_TYPE)"
                           :key="dict.value" :label="dict.label" :value="Number(dict.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="8">-->
          <!--            <el-form-item label="物料属性名称" prop="materialTypeName">-->
          <!--              <el-input v-model="formData.materialTypeName" placeholder="请输入物料属性" style="width: 100%" />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="基本单位" prop="basicUnit">
              <el-select v-model="formData.basicUnit" filterable placeholder="请选择基本单位" clearable style="width: 100%">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MATERIAL_UNIT)"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料重量(kg)" prop="oneMaterialWeight">
              <el-input v-model="formData.oneMaterialWeight" placeholder="请输入一个物料重量(kg)" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否免检" prop="exemption">
              <el-radio-group v-model="formData.exemption">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                          :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="版本号" prop="version">
              <el-input v-model="formData.version" placeholder="请输入版本号" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="安全有效期(天)" prop="safetyDayTime">
              <el-input v-model="formData.safetyDayTime" placeholder="安全有效期(天))" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产节拍(min)" prop="cycleTime">
              <el-input v-model="formData.cycleTime" placeholder="请输入生产节拍(min)" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="安全库存数量" prop="safetyStock">
              <el-input v-model="formData.safetyStock" placeholder="请输入安全库存数量" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最小经济批量" prop="snp">
              <el-input v-model="formData.snp" placeholder="请输入最小经济批量" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格" prop="model">
              <el-input v-model="formData.model" placeholder="请输入规格" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="质检方案" prop="qualitySchemeIds">
              <el-input v-model="formData.qualitySchemeIds" placeholder="请输入绑定的质检方案Id集合" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                          :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述">
              <Editor v-model="formData.description" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as MaterialApi from '@/api/basic/material/index.js';
import Editor from '@/components/Editor';
import {CommonStatusEnum} from '@/utils/constants';
import {DICT_TYPE} from '@/utils/dict';

export default {
  name: "MaterialForm",
  components: {
    Editor
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        materialNo: undefined,
        materialName: undefined,
        materialType: undefined,
        materialTypeName: undefined,
        basicUnit: undefined,
        oneMaterialWeight: undefined,
        description: undefined,
        exemption: CommonStatusEnum.DISABLE, // 修改为默认"否"
        version: undefined,
        safetyDayTime: undefined,
        cycleTime: undefined,
        safetyStock: undefined,
        snp: undefined,
        model: undefined,
        qualitySchemeIds: undefined,
        status: CommonStatusEnum.ENABLE,
      },
      // 表单校验
      formRules: {
        materialNo: [{required: true, message: '物料编码不能为空', trigger: 'blur'}],
        materialName: [{required: true, message: '物料名称不能为空', trigger: 'blur'}],
        materialType: [{required: true, message: '物料属性不能为空', trigger: 'change'}],
        materialTypeName: [{required: true, message: '物料属性名称不能为空', trigger: 'blur'}],
        description: [{required: true, message: '描述不能为空', trigger: 'blur'}],
        version: [{required: true, message: '版本号不能为空', trigger: 'blur'}],
        status: [{required: true, message: '状态不能为空', trigger: 'blur'}],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await MaterialApi.getMaterial(id);
          this.formData = res.data;
          this.dialogTitle = "修改物料";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增物料";
      }
    },

    /** 处理物料属性变更 */
    handleMaterialTypeChange(value) {
      console.log(value)
      this.formData.materialTypeName = this.getDictDataLabel(DICT_TYPE.MATERIAL_TYPE, value)
      console.log(this.formData.materialTypeName)
    },


    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await MaterialApi.updateMaterial(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await MaterialApi.createMaterial(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        materialNo: undefined,
        materialName: undefined,
        materialType: undefined,
        materialTypeName: undefined,
        basicUnit: undefined,
        oneMaterialWeight: undefined,
        description: undefined,
        exemption: CommonStatusEnum.DISABLE, // 修改为默认"否"
        version: undefined,
        safetyDayTime: undefined,
        cycleTime: undefined,
        safetyStock: undefined,
        snp: undefined,
        model: undefined,
        qualitySchemeIds: undefined,
        status: CommonStatusEnum.ENABLE,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
